package cn.example.vip.mall.pay.controller;

import cn.example.vip.mall.pay.entity.PayLog;
import cn.example.vip.mall.util.RespResult;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.Date;

@RestController
@RequestMapping("/wx")
public class WeixinPayController {
	@Autowired
	private RocketMQTemplate rocketMQTemplate;	// 忽略错误
	
	@GetMapping("/result")
	public RespResult result() {
		// 创建 PayLog 对象
		PayLog payLog = new PayLog(IdWorker.getIdStr(), 1, "hello", "AAA", Date.from(Instant.now()));
		// 构建消息对象
		Message<PayLog> message = MessageBuilder.withPayload(payLog).build();
		// 发送消息给 RocketMQ: half-message
		rocketMQTemplate.sendMessageInTransaction("RocketMQ-weixin", "log",  message, null);
		// 事务交给监听器执行，异常则回滚
		return RespResult.ok();
	}
}
